package com.neusoft.databus.policy.repository.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.databus.policy.enums.PolicyType;
import com.neusoft.databus.policy.model.Policy;
import com.neusoft.databus.policy.model.QPolicy;
import com.neusoft.databus.policy.repository.CustomizedPolicyRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedPolicyRepositoryImpl extends AbstractRepository implements CustomizedPolicyRepository {

    @Override
    public Page<Policy> search(Pageable pageable, Searchable searchable) {
        QPolicy qPolicy = QPolicy.policy;
        final BooleanBuilder where = new BooleanBuilder();
        where.and(super.inStrings(qPolicy.code, searchable, "code"));
        where.and(super.containsStr(qPolicy.name, searchable, "name"));
        where.and(super.equalsEnum(qPolicy.policyType, searchable, "policyType", value -> PolicyType.valueOf(value)));
        where.and(super.equalsBool(qPolicy.disabled, searchable, "disabled"));
        JPAQuery<Policy> query = this.query().selectFrom(qPolicy).where(where);
        return super.search(query, pageable);
    }

    @Override
    public List<Policy> search(Searchable searchable) {
        QPolicy qPolicy = QPolicy.policy;
        final BooleanBuilder where = new BooleanBuilder();
        where.and(super.inStrings(qPolicy.code, searchable, "code"));
        where.and(super.containsStr(qPolicy.name, searchable, "name"));
        where.and(super.equalsEnum(qPolicy.policyType, searchable, "policyType", value -> PolicyType.valueOf(value)));
        where.and(super.equalsBool(qPolicy.disabled, searchable, "disabled"));
        JPAQuery<Policy> query = this.query().selectFrom(qPolicy).where(where);
        return query.fetch();
    }

    @Override
    protected Class<?> getModelClass() {
        return Policy.class;
    }

}
